package cn.iocoder.yudao.module.shop.convert.order;

import java.util.*;

import cn.hutool.core.date.DateTime;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.shop.controller.admin.base.member.MemberRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.base.user.UserRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.item.vo.ShopItemRespVO;
import cn.iocoder.yudao.module.shop.convert.base.UserConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderItemDO;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.shop.controller.admin.order.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderDO;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap;

/**
 * 店内消耗订单 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface ShopOrderConvert {

    ShopOrderConvert INSTANCE = Mappers.getMapper(ShopOrderConvert.class);

    default ShopOrderDO convert(ShopOrderCreateReqVO bean)
    {
        ShopOrderDO order = new ShopOrderDO();
        order.setUserId(bean.getUserId());
        order.setMemberId(bean.getMemberId());
        order.setComment(bean.getComment());

        return order;
    }

    ShopOrderDO convert(ShopOrderUpdateReqVO bean);

    ShopOrderRespVO convert(ShopOrderDO bean);

    default ShopOrderRespVO.ShopOrderItemVO ShopOrderItemDOToShopOrderItemVO(ShopOrderItemDO shopOrderItemDO)
    {
        ShopOrderRespVO.ShopOrderItemVO shopOrderItemVO = new ShopOrderRespVO.ShopOrderItemVO();
        shopOrderItemVO.setId(shopOrderItemDO.getId());
        shopOrderItemVO.setOrderId(shopOrderItemDO.getOrderId());
        shopOrderItemVO.setCount(shopOrderItemDO.getCount());
        shopOrderItemVO.setOptItemId(shopOrderItemDO.getOptItemId());
        shopOrderItemVO.setCreateTime(shopOrderItemDO.getCreateTime());

        return shopOrderItemVO;
    }

    default List<ShopOrderRespVO.ShopOrderItemVO> convertToShopOrderItemVOList(List<ShopOrderItemDO> shopOrderItemDOS, Map<Long, ShopItemRespVO> shopItemRespVOMap)
    {
        if(shopOrderItemDOS == null){
            return null;
        }


        List<ShopOrderRespVO.ShopOrderItemVO> list1 = new ArrayList<ShopOrderRespVO.ShopOrderItemVO>( shopOrderItemDOS.size() );
        for ( ShopOrderItemDO shopOrderItemDO : shopOrderItemDOS ) {
            ShopOrderRespVO.ShopOrderItemVO shopOrderItemVO = ShopOrderItemDOToShopOrderItemVO(shopOrderItemDO);
            shopOrderItemVO.setShopItem(shopItemRespVOMap.get(shopOrderItemDO.getOptItemId()));
            list1.add(shopOrderItemVO);
        }

        return list1;
    }

    default ShopOrderRespVO convert(ShopOrderDO order, MemberUserRespDTO memberUserRespDTO){
        ShopOrderRespVO shopOrderRespVO = convert(order);
        shopOrderRespVO.setMember(convertMemberDTO(memberUserRespDTO));

        return shopOrderRespVO;
    }

    default ShopOrderRespVO convert(ShopOrderDO order, List<ShopOrderRespVO.ShopOrderItemVO> items)
    {
        if(order == null && items == null){
            return null;
        }

        ShopOrderRespVO shopOrderRespVO = new ShopOrderRespVO();
        if(order != null)
        {
            shopOrderRespVO.setId(order.getId());
            shopOrderRespVO.setUserId(order.getUserId());
            shopOrderRespVO.setMemberId(order.getMemberId());
            shopOrderRespVO.setTotalPrice(order.getTotalPrice());
            shopOrderRespVO.setPayPrice(order.getPayPrice());
            shopOrderRespVO.setStartTime(order.getStartTime());
            shopOrderRespVO.setEndTime(order.getEndTime());
            shopOrderRespVO.setWalletCharge(order.getWalletCharge());
            shopOrderRespVO.setPayStatus(order.getPayStatus());
            shopOrderRespVO.setCreateTime(order.getCreateTime());
            shopOrderRespVO.setDiscount(order.getDiscount());
            shopOrderRespVO.setComment(order.getComment());
            shopOrderRespVO.setHistoryId(order.getHistoryId());
            shopOrderRespVO.setTimesCharge(order.getTimesCharge());
        }

        shopOrderRespVO.setItems(items);

        return shopOrderRespVO;
    }

    default List<ShopOrderItemDO> convertList(ShopOrderDO order, ShopOrderCreateReqVO createReqVO){
        ShopOrderItemDO shopOrderItemDO = new ShopOrderItemDO();
        shopOrderItemDO.setOrderId(order.getId());
        shopOrderItemDO.setOptItemId(createReqVO.getShopItemId());
        shopOrderItemDO.setCount(createReqVO.getCount());
        shopOrderItemDO.setCreateTime(new DateTime().toLocalDateTime());

        List<ShopOrderItemDO> list = new ArrayList<>();
        list.add(shopOrderItemDO);

        return list;
    }

    List<ShopOrderRespVO> convertList(List<ShopOrderDO> list);

    PageResult<ShopOrderRespVO> convertPage(PageResult<ShopOrderDO> page);

    List<ShopOrderExcelVO> convertList02(List<ShopOrderDO> list);

    default UserRespVO convertUserDTO(AdminUserRespDTO adminUserRespDTO)
    {
        UserRespVO userRespVO = new UserRespVO();
        userRespVO.setId(adminUserRespDTO.getId());
        userRespVO.setNickname(adminUserRespDTO.getNickname());

        return userRespVO;
    }

    default MemberRespVO convertMemberDTO(MemberUserRespDTO memberUserRespDTO)
    {
        MemberRespVO memberRespVO = new MemberRespVO();
        memberRespVO.setId(memberUserRespDTO.getId());
        memberRespVO.setNickname(memberUserRespDTO.getNickname());
        memberRespVO.setAvatar(memberUserRespDTO.getAvatar());
        memberRespVO.setMobile(memberUserRespDTO.getMobile());

        return memberRespVO;
    }

    default  List<ShopOrderRespVO> convertList(List<ShopOrderDO> orderDOList,
                                               List<ShopOrderItemDO> orderItems,
                                               Map<Long, ShopItemRespVO> shopItemRespVOMap,
                                               Map<Long, MemberUserRespDTO> memberUserMap,
                                               Map<Long, AdminUserRespDTO> userMap){

        Map<Long, List<ShopOrderItemDO>> orderItemMap = convertMultiMap(orderItems, ShopOrderItemDO::getOrderId);
        // 转化 List
        List<ShopOrderRespVO> orderVOs = CollectionUtils.convertList(orderDOList, order -> {
            List<ShopOrderItemDO> xOrderItems = orderItemMap.get(order.getId());

            List<ShopOrderRespVO.ShopOrderItemVO> xOrderItemsVo = convertToShopOrderItemVOList(xOrderItems, shopItemRespVOMap);

            ShopOrderRespVO orderVO = convert(order, xOrderItemsVo);
            orderVO.setUser(UserConvert.INSTANCE.convertUserDTO(userMap.get(orderVO.getUserId())));
            orderVO.setMember(convertMemberDTO(memberUserMap.get(orderVO.getMemberId())));

            return orderVO;
        });

        return orderVOs;
    }

    default PageResult<ShopOrderRespVO> convertPage(PageResult<ShopOrderDO> pageResult,
                                                    List<ShopOrderItemDO> orderItems,
                                                    Map<Long, ShopItemRespVO> shopItemRespVOMap,
                                                    Map<Long, MemberUserRespDTO> memberUserMap,
                                                    Map<Long, AdminUserRespDTO> userMap) {

        List<ShopOrderRespVO> orderVOs = convertList(pageResult.getList(), orderItems, shopItemRespVOMap, memberUserMap, userMap);
        return new PageResult<>(orderVOs, pageResult.getTotal());
    }

}
